package com.apestech.framework.sql;

import com.apestech.api.condition.ConditionExec;
import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.db.callback.QueryCallBackHandler;
import com.apestech.framework.db.callback.SaveTableCallBackHandler;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.StringFuncUtil;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.util.callback.SpiltStrCallback;
import com.apestech.framework.xml.condition.modul.Report;

import java.util.ArrayList;

public class ReportSqlSave extends StringFuncUtil implements SaveTableCallBackHandler {

    private Report report;
    private SqlSession session;

    public ReportSqlSave(Report report, SqlSession session){
        this.report=report;
        this.session=session;
    }

    @Override
    public JSONObject updateSql(JSONObject param) throws Exception {
        String upfield="";
        ArrayList<Report.Fields> fields=report.getField();
        int len=fields.size();
        for(int i=0; i<len; i++){
            Report.Fields f=fields.get(i);
            if("output".equals(f.getType()) && f.getField()!=null && f.getExp()!=null && !"".equals(f.getExp().trim())  ){
                param.put(f.getField(), doExpression(f.getExp()));
                if(!"".equals(upfield)){
                    upfield=upfield+",";
                }
                upfield=upfield+f.getField();
            }
        }
        param.put("sys_upfield",upfield);
        return param;
    }

    @Override
    public JSONObject insertSql(JSONObject param) throws Exception {
        String upfield="";
        ArrayList<Report.Fields> fields=report.getField();
        int len=fields.size();
        for(int i=0; i<len; i++){
            Report.Fields f=fields.get(i);
            JSONObject inp=new JSONObject();
            inp=getInput(inp, f, param);
            if("condition".equals(f.getType())){
                inp.put("SYS_RQ","SYSDATE");
                ConditionExec conditionExec=new ConditionExec(session, f.getId());
                JSONObject m=conditionExec.getCondition(inp);
                param=setResult(f, param, m);
            } else if("output".equals(f.getType()) && f.getField()!=null && f.getExp()!=null && !"".equals(f.getExp().trim()) ){
                param.put(f.getField(), doExpression(f.getExp()));
                if(!"".equals(upfield)){
                    upfield=upfield+",";
                }
                upfield=upfield+f.getField();
            }
        }
        param.put("sys_upfield",upfield);
        return param;
    }

    private JSONObject getInput(JSONObject inp, Report.Fields f, JSONObject param) throws Exception {
        String input=f.getInputs();
        if(input!=null){
            JSONObject finalInp = inp;
            execSplitStr(input, ";", new SpiltStrCallback() {
                @Override
                public void exec(String id) throws Exception {
                    finalInp.put(id, param.getString(id));
                }
            });
            inp.putAll(finalInp);
        }
        ArrayList<Report.OutField> oflist=f.getOutfield();
        int flen=oflist.size();
        for(int j=0; j<flen; j++){
            String key=oflist.get(j).getField();
            input=oflist.get(j).getInput();
            if(input!=null){
                String sql=oflist.get(j).getExpress();
                if(sql!=null && !"".equals(sql.trim())){
                    param.put("autoCompletion","0");
                    param.put("nb_from",oflist.get(j).getFrom());
                    session.getForResultSet().sql(sql, param, new QueryCallBackHandler() {
                        @Override
                        public void getData(SqlResultSet rs, JSONArray rslist) throws Exception {
                            if(rs.next()){
                                inp.put(key, rs.getObject(1));
                            }
                        }
                    });
                    param.remove("autoCompletion");
                    param.remove("nb_from");
                } else {
                    inp.put(key, param.getString(input));
                }
            }
        }
        return inp;
    }

    private JSONObject setResult(Report.Fields f, JSONObject param, JSONObject m) throws Exception {
        ArrayList<Report.OutField> oflist=f.getOutfield();
        int flen=oflist.size();
        for(int j=0; j<flen; j++){
            String key=oflist.get(j).getField();
            String result=oflist.get(j).getResult();
            if(result!=null){
                String exp=oflist.get(j).getExpress();
                if(exp==null || "".equals(exp.trim())){
                    param.put(key, m.getString(result));
                } else {
                    param.put(key, doExpression(exp, m, param));
                }

            }
        }
        return param;
    }
}
